﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Auction_Server.Business;
using System.Data;

namespace Auction_Server
{
    /// <summary>
    /// 拍卖会选择页面
    /// </summary>
    public partial class StartWindow : Window
    {
        public StartWindow()
        {
            if(!App.MySer.IsRun){
                //启动服务器,客户端可以建立连接,但不可以登录
                App.startServer();
            }

            //初始化终端表
            Business.baseDataService baseService = new Business.baseDataService();
            App.htTerminal = baseService.SetTerminalMap();
            App.htBidCodeToName = baseService.getAllDict();

            //判断待拍卖会数量,==1默认进入
            auctionAction auction = new auctionAction();
            App.todayAuction = auction.getTodayAuctionLst();
            if (App.todayAuction.Rows.Count == 1)
            {
                App.currentAuctionNo = App.todayAuction.Rows[0]["AUCTION_NO"].ToString();
                //显示主窗体
                AppWindow mainWin = new AppWindow();
                mainWin.Show();
                //关闭本窗体
                this.Close();
            }
            else
            {
                //构建窗体事件
                InitializeComponent();

                auctionLV.DataContext = App.todayAuction;
                //既没指定Source又没指定Path的Binding
                auctionLV.SetBinding(ListView.ItemsSourceProperty, new Binding());
            }
        }

        private void btnEnter_Click(object sender, RoutedEventArgs e)
        {
            DataRowView dataRow = (DataRowView)this.auctionLV.SelectedItem;
            if (dataRow == null)
            {
                MessageBox.Show("请选择拍卖会");
            }
            else
            {
                App.currentAuctionNo = dataRow.Row["AUCTION_NO"].ToString();
                this.Hide();

                //显示主窗体
                AppWindow mainWin = new AppWindow();
                mainWin.Show();

                //关闭本窗体
                this.Close();
            }
        }

        /// <summary>
        /// 窗体拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left) this.DragMove();
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExitApp_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            App.Current.Shutdown();
        }
    }
}
